package com.ww.basic.oop

import scala.collection.mutable

class Marker(val color: String) {
  println(s"Creating ${this}")

  override def toString = s"marker color $color"
}

object MarkerFactory {
  private val markers = mutable.Map(
    "red" -> new Marker("red"),
    "blue" -> new Marker("blue"),
    "yellow" -> new Marker("yellow"))

  def getMarker(color: String): Marker =
    markers.getOrElseUpdate(color, new Marker(color))
}

object TestMarker {
  def main(args: Array[String]): Unit = {
    println(MarkerFactory.getMarker("blue"))
    println(MarkerFactory.getMarker("blue"))
    println(MarkerFactory.getMarker("red"))
    println(MarkerFactory.getMarker("red"))
    println(MarkerFactory.getMarker("green"))
  }
}